package com.mzxy.boxuegu.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.boxuegu.utils.Constants;
import com.mzxy.boxuegu.utils.MD5Utils;
import com.mzxy.boxuegu.utils.SpUtils;

public class LoginActivity extends AppCompatActivity implements OnClickListener{

    private EditText etUserName;
    private EditText etPwd;
    private Button btnLogin;
    private TextView tvRegister;
    private TextView tvFindPsw;
    private TextView tvMainTitle;
    private TextView tvBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initEvent();
    }

    private void initEvent() {
        btnLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        tvFindPsw.setOnClickListener(this);
        tvBack.setOnClickListener(this);
    }

    private void initView() {
        etUserName = (EditText) findViewById(R.id.et_user_name);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        btnLogin = (Button) findViewById(R.id.btn_login);
        tvRegister = (TextView) findViewById(R.id.tv_register);
        tvFindPsw = (TextView) findViewById(R.id.tv_find_psw);
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        tvMainTitle.setText("登陆");
        tvBack = (TextView) findViewById(R.id.tv_back);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_register:
                startActivityForResult(new Intent(this,RegisterActivity.class),0x001);
                break;
            case R.id.tv_find_psw:
                startActivity(new Intent(this,FindPswActivity.class));
                break;
            case R.id.tv_back:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String username = data.getStringExtra("username");
        String password = data.getStringExtra("password");
        etUserName.setText(username);
        etPwd.setText(password);
    }

    private void login() {
        String username = etUserName.getText().toString();
        String password = etPwd.getText().toString();
        //先判断是否为空
        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
            Toast.makeText(this,"账号和密码均不能为空！",Toast.LENGTH_SHORT).show();
            return;
        }

        //如果根据用户名，能获取到密码，则证明用户已经注册
        String oldPwdMD5 = SpUtils.getPsw(this,username);
        if(TextUtils.isEmpty(oldPwdMD5)){
            Toast.makeText(this,"该用户名未注册！",Toast.LENGTH_SHORT).show();
            return;
        }

        //比较密码的MD5
        String pwdmd5 = MD5Utils.md5(password);

        if(oldPwdMD5.equals(pwdmd5)){
            SpUtils.saveLoginStatus(this,true);
            SpUtils.saveLoginUsername(this,username);
            Intent data = new Intent();
            data.putExtra("username",username);
            setResult(Constants.LOGIN2MYINFO_RESULT_CODE,data);
            Toast.makeText(this,"登陆成功！",Toast.LENGTH_SHORT).show();
            finish();
        }else {
            SpUtils.saveLoginStatus(this,false);
            SpUtils.saveLoginUsername(this,"");
            Toast.makeText(this,"密码错误！",Toast.LENGTH_SHORT).show();
        }
    }

}
